/*******************************************************************************

This code file produces Table A13, "Robustness to Lot Area Threshold."

*******************************************************************************/

*** Manage settings

	run "$dir/code/modules/settings.do"
	
*** Load Stata-TeX

	do "$code/modules/stata-tex.do"
	cd "$tables/estimates"

********************************************************************************
* 20% threshold
********************************************************************************
	
*** Prepare simulated instrument

	use "$data/raw/matched_buildings.dta", clear
	gcollapse (mean) dtaxrate_onsite, by(bbl)
	rename dtaxrate_onsite dtaxrate_onsite_sim
	
	tempfile sim
	save `sim', replace
	
*** Load data

	use "$data/clean/cleaned_data.dta", clear
	
	merge 1:1 bbl using `sim', nogen keep(1 3)
	
	gen constant = 1
	
*** Logit, borough FEs, expanding controls

	* No controls
	
	gmm (inclusionary_onsite - invlogit({xb:dtaxrate_onsite i.borough i.yearpermit constant})), ///
		instruments(dtaxrate_onsite_sim i.borough i.yearpermit) vce(cluster nta)
	store_est_tpl using tableA13.csv, coef(dtaxrate_onsite) name(logit1_20) all
		
	* Lot controls
	
	gmm (inclusionary_onsite - invlogit({xb:dtaxrate_onsite $controls_lot i.borough i.yearpermit constant})), ///
		instruments(dtaxrate_onsite_sim $controls_lot i.borough i.yearpermit) vce(cluster nta)
	store_est_tpl using tableA13.csv, coef(dtaxrate_onsite) name(logit2_20) all
		
	* Block controls
	
	logit inclusionary_onsite dtaxrate_onsite $controls_block i.borough i.yearpermit, cl(nta) asis
	mat b_init = e(b)
			
	gmm (inclusionary_onsite - invlogit({xb:dtaxrate_onsite $controls_block i.borough i.yearpermit constant})), ///
		instruments(dtaxrate_onsite_sim $controls_block i.borough i.yearpermit) vce(cluster nta) ///
		conv_maxiter(10) from(b_init) quickderivatives
	store_est_tpl using tableA13.csv, coef(dtaxrate_onsite) name(logit3_20) all

	* Lot + block controls
	
	logit inclusionary_onsite dtaxrate_onsite $controls_lot $controls_block i.borough i.yearpermit, cl(nta) asis
	mat b_init = e(b)
			
	gmm (inclusionary_onsite - invlogit({xb:dtaxrate_onsite $controls_lot $controls_block i.borough i.yearpermit constant})), ///
		instruments(dtaxrate_onsite_sim $controls_lot $controls_block i.borough i.yearpermit) vce(cluster nta) ///
		conv_maxiter(10) from(b_init) quickderivatives
	store_est_tpl using tableA13.csv, coef(dtaxrate_onsite) name(logit4_20) all	
	
********************************************************************************
* 10% threshold
********************************************************************************
	
*** Prepare simulated instrument

	use "$data/raw/matched_buildings_alt1.dta", clear
	gcollapse (mean) dtaxrate_onsite, by(bbl)
	rename dtaxrate_onsite dtaxrate_onsite_sim
	
	tempfile sim
	save `sim', replace
	
*** Load data

	use "$data/clean/cleaned_data.dta", clear
	
	merge 1:1 bbl using `sim', nogen keep(1 3)
	
	gen constant = 1
	
*** Logit, borough FEs, expanding controls

	* No controls
	
	gmm (inclusionary_onsite - invlogit({xb:dtaxrate_onsite i.borough i.yearpermit constant})), ///
		instruments(dtaxrate_onsite_sim i.borough i.yearpermit) vce(cluster nta)
	store_est_tpl using tableA13.csv, coef(dtaxrate_onsite) name(logit1_10) all
		
	* Lot controls
	
	gmm (inclusionary_onsite - invlogit({xb:dtaxrate_onsite $controls_lot i.borough i.yearpermit constant})), ///
		instruments(dtaxrate_onsite_sim $controls_lot i.borough i.yearpermit) vce(cluster nta)
	store_est_tpl using tableA13.csv, coef(dtaxrate_onsite) name(logit2_10) all
		
	* Block controls
	
	logit inclusionary_onsite dtaxrate_onsite $controls_block i.borough i.yearpermit, cl(nta) asis
	mat b_init = e(b)
			
	gmm (inclusionary_onsite - invlogit({xb:dtaxrate_onsite $controls_block i.borough i.yearpermit constant})), ///
		instruments(dtaxrate_onsite_sim $controls_block i.borough i.yearpermit) vce(cluster nta) ///
		conv_maxiter(10) from(b_init) quickderivatives
	store_est_tpl using tableA13.csv, coef(dtaxrate_onsite) name(logit3_10) all

	* Lot + block controls
	
	logit inclusionary_onsite dtaxrate_onsite $controls_lot $controls_block i.borough i.yearpermit, cl(nta) asis
	mat b_init = e(b)
			
	gmm (inclusionary_onsite - invlogit({xb:dtaxrate_onsite $controls_lot $controls_block i.borough i.yearpermit constant})), ///
		instruments(dtaxrate_onsite_sim $controls_lot $controls_block i.borough i.yearpermit) vce(cluster nta) ///
		conv_maxiter(10) from(b_init) quickderivatives
	store_est_tpl using tableA13.csv, coef(dtaxrate_onsite) name(logit4_10) all	
	
********************************************************************************
* 30% threshold
********************************************************************************
	
*** Prepare simulated instrument

	use "$data/raw/matched_buildings_alt2.dta", clear
	gcollapse (mean) dtaxrate_onsite, by(bbl)
	rename dtaxrate_onsite dtaxrate_onsite_sim
	
	tempfile sim
	save `sim', replace
	
*** Load data

	use "$data/clean/cleaned_data.dta", clear
	
	merge 1:1 bbl using `sim', nogen keep(1 3)
	
	gen constant = 1
	
*** Logit, borough FEs, expanding controls

	* No controls
	
	gmm (inclusionary_onsite - invlogit({xb:dtaxrate_onsite i.borough i.yearpermit constant})), ///
		instruments(dtaxrate_onsite_sim i.borough i.yearpermit) vce(cluster nta)
	store_est_tpl using tableA13.csv, coef(dtaxrate_onsite) name(logit1_30) all
		
	* Lot controls
	
	gmm (inclusionary_onsite - invlogit({xb:dtaxrate_onsite $controls_lot i.borough i.yearpermit constant})), ///
		instruments(dtaxrate_onsite_sim $controls_lot i.borough i.yearpermit) vce(cluster nta)
	store_est_tpl using tableA13.csv, coef(dtaxrate_onsite) name(logit2_30) all
		
	* Block controls
	
	logit inclusionary_onsite dtaxrate_onsite $controls_block i.borough i.yearpermit, cl(nta) asis
	mat b_init = e(b)
			
	gmm (inclusionary_onsite - invlogit({xb:dtaxrate_onsite $controls_block i.borough i.yearpermit constant})), ///
		instruments(dtaxrate_onsite_sim $controls_block i.borough i.yearpermit) vce(cluster nta) ///
		conv_maxiter(10) from(b_init) quickderivatives
	store_est_tpl using tableA13.csv, coef(dtaxrate_onsite) name(logit3_30) all

	* Lot + block controls
	
	logit inclusionary_onsite dtaxrate_onsite $controls_lot $controls_block i.borough i.yearpermit, cl(nta) asis
	mat b_init = e(b)
			
	gmm (inclusionary_onsite - invlogit({xb:dtaxrate_onsite $controls_lot $controls_block i.borough i.yearpermit constant})), ///
		instruments(dtaxrate_onsite_sim $controls_lot $controls_block i.borough i.yearpermit) vce(cluster nta) ///
		conv_maxiter(10) from(b_init) quickderivatives
	store_est_tpl using tableA13.csv, coef(dtaxrate_onsite) name(logit4_30) all	
	
********************************************************************************
* Create table
********************************************************************************
	
	cat tableA13.csv

	cap erase "$tables/tableA13.tex"

	cd "$code/modules"
	
	capture table_from_tpl, t("$tables/templates/tableA13.tex") ///
						r("$tables/estimates/tableA13.csv") ///
						o("$tables/output/tableA13.tex") 
						
	capture table_from_tpl, t("$tables/templates/tableA13.tex") ///
						r("$tables/estimates/tableA13.csv") ///
						o("$tables_overleaf/tableA13.tex") 
		
		
